package com.commerceerp.system.controller;

import com.commerceerp.system.service.ImageService;
import com.commerceerp.system.service.LoginService;
import com.commerceerp.system.util.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;


/**
 * Created by Administrator on 2018/10/29 0029.
 */
@Api(description = "图片管理", value = "图片管理接口")
@RestController
@RequestMapping("/img")
public class ImageController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private ImageService imageService;

    @ApiOperation(value = "上传图片文件")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult uploadFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }

        String userId = to.getData().toString();
        return imageService.addFile(file,userId);
    }

    @ApiOperation(value = "删除线上文件")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult fileDelete(String id,HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String userId = to.getData().toString();
        return imageService.fileDelete(id,userId);
    }

    @ApiOperation(value = "查看图片")
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult findList(Integer page,Integer limit,HttpServletRequest request) {
        if(page == null){
            page = 1;
        }
        if(limit == null){
            limit = 10;
        }
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String userId = to.getData().toString();
        return imageService.findList(userId,page,limit);
    }

    @ApiOperation(value = "查看图片（上传商品调用的接口）")
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getList(Integer page,Integer limit,HttpServletRequest request) {
        if(page == null){
            page = 1;
        }
        if(limit == null){
            limit = 10;
        }
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String userId = to.getData().toString();
        return imageService.getList(userId,page,limit);
    }

    @ApiOperation(value = "图片空间")
    @RequestMapping(value = "/imgSpace", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult imgSpace(HttpServletRequest request) {
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String userId = to.getData().toString();
        return imageService.imgSpace(userId);
    }

    @ApiOperation(value = "扩容空间")
    @RequestMapping(value = "/addSpace", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addSpace(Integer size,int type,HttpServletRequest request) {
        if(size == null || size < 100){
            return new ApiResult(405,"扩容量必须是100以上整数");
        }
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String userId = to.getData().toString();
        return imageService.addSpace(size,type,userId);
    }

}
